package Common;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import Commande.Command;

public class HorlogeImpl implements Horloge{

	Map<Command, Timer> mapTimer = new HashMap<Command, Timer>(); 

	public HorlogeImpl(){
	}

	@Override
	public void activerPeriodiquement(final Command cmd, float periodeEnSeconde) {
		
		Timer timerTmp = new Timer();
		timerTmp.schedule(new TimerTask() {

			@Override
			public void run() {
				cmd.execute();
			}
		}, 0, (long) periodeEnSeconde);
		mapTimer.put(cmd, timerTmp);
	}

	@Override
	public void activerApresDelai(final Command cmd, float delaiEnSecondes) {
		Timer timerTmp = new Timer();		
		timerTmp.schedule(new TimerTask() {

			@Override
			public void run() {
				cmd.execute();
			}
		}, (long) delaiEnSecondes);
		mapTimer.put(cmd, timerTmp);
	}

	@Override
	public void desactiver(Command cmd) {
		Timer timerTemp = mapTimer.get(cmd);
		timerTemp.cancel();
		timerTemp.purge();
		mapTimer.remove(cmd);
	}
}
